package com.freestyle.seatabusiness.authorization.config;

import com.freestyle.easyshiro.realms.MyAuthenticationInfo;
import com.freestyle.easyshiro.realms.MyAuthenticationInfoService;
import com.freestyle.easyshiro.realms.UserDtoWithPermission;
import com.freestyle.easyshiro.realms.UsernamePasswordTokenEx;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Created by rocklee on 2022/3/10 17:40
 */
@Service
public class MyAuthenticationInfoServiceImpl implements MyAuthenticationInfoService {
  private Map<String,MyAuthenticationInfo> accounts=new HashMap<>();
  public MyAuthenticationInfoServiceImpl() {
    accounts.put("user1",new MyAuthenticationInfo(new UserDtoWithPermission("user1",new HashSet(){{add("ROLE1");add("ROLE2");}},
            new HashSet(){{add("P1");}}),"123456","accountRealm"));
    accounts.put("user2",new MyAuthenticationInfo(new UserDtoWithPermission("user2",new HashSet(){{add("ROLE1");add("ROLE2");}},
            new HashSet(){{add("P1");add("P2");}}),"123456","accountRealm"));
  }

  @Override
  public MyAuthenticationInfo login(UsernamePasswordTokenEx token) {
    return accounts.get(token.getUsername());
  }

  @Override
  public AuthorizationInfo getAuthorization(UserDtoWithPermission principal) {
    return principal;
  }
}
